#! /usr/bin/env bash

if [ "${TRAVIS}" == "true" ]
then
  BUILDDIR=${1:-build}
  mkdir $BUILDDIR && cd $BUILDDIR
else
  EXTRA_ARGS=$@
fi

### The following assumes you are building in a subdirectory of ACCESS Root
### If not, then define "ACCESS" to point to the root of the SEACAS source code.
if [ "$ACCESS" == "" ]
then
    ACCESS=$(cd ..; pwd)
fi

### The SEACAS code will install in ${INSTALL_PATH}/bin, ${INSTALL_PATH}/lib, and ${INSTALL_PATH}/include.
INSTALL_PATH=${INSTALL_PATH:-${ACCESS}}

### TPLs could be installed separately from SEACAS.  Default to INSTALL_PATH.
TPL_INSTALL=${TPL_INSTALL:-${INSTALL_PATH}}

### Possible subset of what is built ---
APPLICATIONS=${APPLICATIONS:-YES}
LEGACY=${LEGACY:-YES}
FORTRAN=${FORTRAN:-YES}
ZOLTAN=${ZOLTAN:-YES}

### Python Version...
PYTHON_VER=${PYTHON_VER:-"3.0"}


### Set to YES for parallel compile; otherwise NO for serial (default)
netcdf_parallel=$($NETCDF_ROOT/bin/nc-config --has-parallel)
if [ "${netcdf_parallel}" == "yes" ]
then
    MPI=YES
else
    MPI=NO
fi

echo "MPI set to ${MPI}"

if [ "${MPI}" == "NO" ]
then
  ### Change this to point to the compilers you want to use
    ## Travis build (and others) set this to EXTERNAL to set
    ## CXX, CC, and FC externally.
  COMPILER=${COMPILER:-gnu}

  if [ "$COMPILER" == "gnu" ]
  then
      CXX=g++
      CC=gcc
      FC=gfortran
      CFLAGS="-Wall -Wunused -pedantic"
      CXXFLAGS="-Wall -Wunused -pedantic"
  fi

  if [ "$COMPILER" == "clang" ]
  then
      CXX=clang++
      CC=clang
      FC=gfortran-mp-7
      CFLAGS="-Wall -Wunused -pedantic"
      CXXFLAGS="-Wall -Wunused -pedantic"
  fi

  if [ "$COMPILER" == "intel" ]
  then
      CXX=icpc
      CC=icc
      FC=ifort
      CFLAGS="-Wall -Wunused"
      CXXFLAGS="-Wall -Wunused"
  fi

  if [ "$COMPILER" == "ibm" ]
  then
      CXX=xlC
      CC=xlc
      FC=xlf
  fi

fi

if [ "${APPLICATIONS}" == "YES" ] && [ "${LEGACY}" == "YES" ]
then
     SUBSET_OPTIONS="-DSeacas_ENABLE_ALL_PACKAGES:BOOL=ON \
                     -DSeacas_ENABLE_ALL_OPTIONAL_PACKAGES:BOOL=ON \
                     -DSeacas_ENABLE_SECONDARY_TESTED_CODE:BOOL=ON"
else
     SUBSET_OPTIONS="-DSeacas_ENABLE_ALL_PACKAGES:BOOL=OFF \
                     -DSeacas_ENABLE_ALL_OPTIONAL_PACKAGES:BOOL=OFF \
		     -DSeacas_ENABLE_SECONDARY_TESTED_CODE:BOOL=OFF \
                     -DSeacas_ENABLE_SEACASIoss:BOOL=ON \
		     -DSeacas_ENABLE_SEACASExodus:BOOL=ON \
		     -DSeacas_ENABLE_SEACASExodus_for:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExoIIv2for32:BOOL=ON"

if [ "${APPLICATIONS}" == "YES" ]
then
     SUBSET_OPTIONS="${SUBSET_OPTIONS} \
                     -DSeacas_ENABLE_SEACASAprepro:BOOL=ON \
                     -DSeacas_ENABLE_SEACASAprepro_lib:BOOL=ON \
                     -DSeacas_ENABLE_SEACASConjoin:BOOL=ON \
                     -DSeacas_ENABLE_SEACASEjoin:BOOL=ON \
                     -DSeacas_ENABLE_SEACASEpu:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExo2mat:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExo_format:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExodiff:BOOL=ON \
                     -DSeacas_ENABLE_SEACASMat2exo:BOOL=ON \
                     -DSeacas_ENABLE_SEACASNemslice:BOOL=ON \
                     -DSeacas_ENABLE_SEACASNemspread:BOOL=ON"

     if [ "${FORTRAN}" == "YES" ]
	then
	    SUBSET_OPTIONS="${SUBSET_OPTIONS} \
            		  -DSeacas_ENABLE_SEACASExplore:BOOL=ON \
			  -DSeacas_ENABLE_SEACASGrepos:BOOL=ON"
     fi

elif [ "${LEGACY}" == "YES" ] && [ "${FORTRAN}" == "YES" ]
then
     SUBSET_OPTIONS="${SUBSET_OPTIONS} \
                     -DSeacas_ENABLE_SEACASAlgebra:BOOL=ON \
                     -DSeacas_ENABLE_SEACASBlot:BOOL=ON \
                     -DSeacas_ENABLE_SEACASEx1ex2v2:BOOL=ON \
                     -DSeacas_ENABLE_SEACASEx2ex1v2:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExomatlab:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExotec2:BOOL=ON \
                     -DSeacas_ENABLE_SEACASExotxt:BOOL=ON \
                     -DSeacas_ENABLE_SEACASFastq:BOOL=ON \
                     -DSeacas_ENABLE_SEACASGen3D:BOOL=ON \
                     -DSeacas_ENABLE_SEACASGenshell:BOOL=ON \
                     -DSeacas_ENABLE_SEACASGjoin:BOOL=ON \
                     -DSeacas_ENABLE_SEACASMapvar:BOOL=ON \
                     -DSeacas_ENABLE_SEACASMapvar-kd:BOOL=ON \
                     -DSeacas_ENABLE_SEACASNemesis:BOOL=ON \
                     -DSeacas_ENABLE_SEACASTxtexo:BOOL=ON"
fi
fi

GENERATOR=${GENERATOR:-"Unix Makefiles"}

# If using an XLF compiler on an IBM system, may need to add the following:
# -DCMAKE_Fortran_FLAGS="-qfixed=72" \
# -DCMAKE_EXE_LINKER_FLAGS:STRING="-lxl -lxlopt"

SHARED="${SHARED:-YES}"

### Switch for Debug or Release build:
### Check that both `DEBUG` and `BUILD_TYPE` are not set
if [ ! -z ${DEBUG+x} ] && [ ! -z ${BUILD_TYPE+x} ]
then
    echo "ERROR: Both DEBUG and BUILD_TYPE are set. Only one is allowed."
    exit
fi

BUILD_TYPE="${BUILD_TYPE:-RELEASE}"

if [ ! -z ${DEBUG+x} ]
then
    if [ "${DEBUG}" == "YES" ] 
    then
	BUILD_TYPE="DEBUG"
    elif [ "${DEBUG}" == "NO" ] 
    then
	BUILD_TYPE="RELEASE"
    else
	echo "ERROR: Invalid value for DEBUG ('$DEBUG'). Must be 'YES' or 'NO'."
	exit
    fi
fi


### If you do not have the X11 developer package on your system
### which provides X11/Xlib.h and the libX11, then change the "YES"
### below to "NO". It will disable blot and fastq
HAVE_X11=${X11:-YES}

### Set to YES to enable the building of a thread-safe version of the Exodus and IOSS libraries.
THREADSAFE=${THREADSAFE:-NO}
if [ "$THREADSAFE" == "YES" ] ; then
  THREAD_SAFE_OPT="-DSeacas_EXTRA_LINK_FLAGS=-lpthread"
fi

if [ "${FAODEL_INSTALL}" == "" ]; then
  # FAODEL_INSTALL isn't set.  Try to derive it from Faodel_DIR.
  if [ -e ${Faodel_DIR}/Faodel/FaodelConfig.cmake ]; then
    # Faodel_DIR points to the cmake directory, so trim it.
    FAODEL_INSTALL=${Faodel_DIR}/../..
  elif [ -e ${Faodel_DIR}/lib/cmake/Faodel/FaodelConfig.cmake ]; then
    # Faodel_DIR points to the install root, so just use it.
    FAODEL_INSTALL=${Faodel_DIR}
  fi
fi

### TPLs --
### Make sure these point to the locations to find the libraries and includes in lib and include
### subdirectories of the specified paths.
### For example, netcdf.h should be in ${NETCDF_PATH}/include
NETCDF_PATH=${NETCDF_ROOT:-${TPL_INSTALL}}
PNETCDF_PATH=${PNETCDF_ROOT:-${TPL_INSTALL}}
MATIO_PATH=${MATIO_ROOT:-${TPL_INSTALL}}
HDF5_PATH=${HDF5_ROOT:-${TPL_INSTALL}}
CGNS_PATH=${CGNS_ROOT:-${TPL_INSTALL}}
CEREAL_PATH=${CEREAL_ROOT:-${TPL_INSTALL}}
FAODEL_PATH=${FAODEL_ROOT:-${FAODEL_INSTALL}}
ADIOS2_PATH=${ADIOS2_ROOT:-${TPL_INSTALL}}
GTEST_PATH=${GTEST_ROOT:-${TPL_INSTALL}}
KOKKOS_PATH=${KOKKOS_ROOT:-${TPL_INSTALL}}

function check_enable()
{
    local path=$1
    if [ -e "${path}" ]
    then
	echo "YES"
    else
	echo "NO"
    fi
}

HAVE_NETCDF=$(check_enable "${NETCDF_PATH}/include/netcdf.h")
HAVE_MATIO=$(check_enable "${MATIO_PATH}/include/matio.h")
HAVE_CGNS=$(check_enable "${CGNS_PATH}/include/cgnslib.h")
HAVE_CEREAL=$(check_enable "${CEREAL_PATH}/include/cereal/cereal.hpp")
HAVE_FAODEL=$(check_enable "${FAODEL_PATH}/lib/cmake/Faodel/FaodelConfig.cmake")
HAVE_ADIOS2=$(check_enable "${ADIOS2_PATH}/include/adios2.h")
HAVE_GTEST=$(check_enable "${GTEST_PATH}/include/gtest/gtest.h")
HAVE_KOKKOS=$(check_enable "${KOKKOS_PATH}/include/Kokkos_Core.hpp")


### DataWarp (Burst Buffer)
### I use the following for mutrino (10/16/2018):
###    module load datawarp
###    -D TPL_ENABLE_DataWarp:BOOL=ON \
###    -D DataWarp_LIBRARY_DIRS:PATH=/opt/cray/datawarp/2.1.16-6.0.5.1_2.61__g238b34d.ari/lib \
###    -D DataWarp_INCLUDE_DIRS:PATH=/opt/cray/datawarp/2.1.16-6.0.5.1_2.61__g238b34d.ari/include \

### Define to NO to *enable* exodus deprecated functions
OMIT_DEPRECATED_CODE="NO"

# BUG needs to work with cray too.
if [ "${MPI}" == "YES" ] && [ "${CRAY}" == "YES" ]
then
   MPI_EXEC=$(which srun)
   MPI_SYMBOLS="-D MPI_EXEC=${MPI_EXEC} -D MPI_EXEC_NUMPROCS_FLAG=-n"
   CXX=CC
   CC=cc
   FC=ftn
   MPI_BIN=$(dirname $(which ${CC}))
   SHARED=NO
   unset CRAYPE_LINK_TYPE
elif [ "${MPI}" == "YES" ]
then
   if [ "${USE_SRUN}" == "YES" ]
   then
       MPI_EXEC=$(which srun)
       MPI_SYMBOLS="-D MPI_EXEC=${MPI_EXEC} -D MPI_EXEC_NUMPROCS_FLAG=-N"
       MPI_BIN=$(dirname "${MPI_EXEC}")
   else
       MPI_EXEC=$(which mpiexec)
       MPI_SYMBOLS="-D MPI_EXEC=${MPI_EXEC}"
       MPI_BIN=$(dirname "${MPI_EXEC}")
   fi
   CXX=mpicxx
   CC=mpicc
   FC=mpif77
fi

OS=$(uname -s)
if [ "$SHARED" == "YES" ]
then
    if [ "$OS" == "Darwin" ] ; then
	LD_EXT="dylib"
    else
	LD_EXT="so"
    fi
else
    LD_EXT="a"
fi

if [ "${HAVE_KOKKOS}" == "YES" ]
then
    KOKKOS_SYMBOLS="-DKOKKOS_SRC_PATH:PATH=${INSTALL_PATH}/TPL/kokkos/kokkos \
		    -DTPL_Kokkos_LIBRARY_DIRS:PATH=${KOKKOS_PATH}/lib                \
		    -DTPL_Kokkos_INCLUDE_DIRS:PATH=${KOKKOS_PATH}/include            \
		    -DTPL_Kokkos_LIBRARIES=${KOKKOS_PATH}/lib/libkokkos.${LD_EXT}"
fi

if [ "$HAVE_CEREAL" == "YES" ]
then
  CEREAL_SYMBOLS=" -D TPL_ENABLE_Cereal:BOOL=${HAVE_CEREAL}   \
    -D Cereal_INCLUDE_DIRS:PATH=${CEREAL_PATH}/include \
    "
fi

if [ "$HAVE_FAODEL" == "YES" ]
then
  export Faodel_DIR

  FAODEL_SYMBOLS=" -D TPL_ENABLE_Faodel:BOOL=${HAVE_FAODEL}   \
    -D Faodel_ROOT:PATH=${FAODEL_PATH} \
    "
fi
#-D TRIBITS_TPL_FIND_INCLUDE_DIRS_AND_LIBRARIES_VERBOSE=TRUE \
#-D Faodel_FORCE_PRE_FIND_PACKAGE:BOOL=TRUE \
#    -D Faodel_INCLUDE_DIRS:PATH=${FAODEL_PATH}/include/faodel \
#    -D Faodel_LIBRARY_DIRS:PATH=${FAODEL_PATH}/lib
    
if [ "$OS" == "Darwin" ] ; then
  DARWIN_OPT="-D CMAKE_MACOSX_RPATH:BOOL=ON -D TPL_X11_INCLUDE_DIRS:PATH=/opt/X11/include"
else
  DARWIN_OPT=""
fi

# Only run doxygen if me and on master branch...
DOXYGEN=NO
if [[ "$OS" == "Darwin" && "$MPI" == "NO" ]] ; then
    branch=$(git branch |grep \* |cut -c3-)
    USER=$(id -nu)
    if [ "$USER" == "gdsjaar" ] && [ "$branch" == "master" ]; then
	DOXYGEN=YES
    fi
fi

FC=${FC:-gfortran}

rm -f CMakeCache.txt

###------------------------------------------------------------------------
cmake -G "${GENERATOR}" \
-D CMAKE_CXX_COMPILER:FILEPATH=${CXX} \
-D CMAKE_C_COMPILER:FILEPATH=${CC} \
-D CMAKE_Fortran_COMPILER:FILEPATH=${FC} \
-D CMAKE_CXX_FLAGS=${CXXFLAGS} ${SANITIZER} \
-D CMAKE_C_FLAGS=${CFLAGS} ${SANITIZER} \
-D CMAKE_INSTALL_RPATH:PATH=${INSTALL_PATH}/lib \
-D BUILD_SHARED_LIBS:BOOL=${SHARED} \
-D CMAKE_BUILD_TYPE=${BUILD_TYPE} \
${SUBSET_OPTIONS} \
-D Seacas_ENABLE_Zoltan:BOOL=${ZOLTAN} \
-D Seacas_ENABLE_TESTS=ON \
-D CMAKE_INSTALL_PREFIX:PATH=${INSTALL_PATH} \
-D Seacas_SKIP_FORTRANCINTERFACE_VERIFY_TEST:BOOL=ON \
-D Seacas_HIDE_DEPRECATED_CODE:BOOL=${OMIT_DEPRECATED_CODE} \
-D Seacas_ENABLE_DOXYGEN:BOOL=${DOXYGEN} \
-D Seacas_ENABLE_Fortran=${FORTRAN} \
\
-D TPL_ENABLE_Netcdf:BOOL=${HAVE_NETCDF} \
-D TPL_ENABLE_Matio:BOOL=${HAVE_MATIO} \
-D TPL_ENABLE_CGNS:BOOL=${HAVE_CGNS} \
-D TPL_ENABLE_ADIOS2:BOOL=${HAVE_ADIOS2} \
-D TPL_ENABLE_gtest:BOOL=${HAVE_GTEST} \
-D TPL_ENABLE_Kokkos:BOOL=${HAVE_KOKKOS} \
-D TPL_ENABLE_MPI:BOOL=${MPI} \
-D TPL_ENABLE_Pamgen:BOOL=OFF \
-D TPL_ENABLE_Pthread:BOOL=${THREADSAFE} \
${THREAD_SAFE_OPT} \
-D TPL_ENABLE_X11:BOOL=${HAVE_X11} \
\
-D SEACASExodus_ENABLE_THREADSAFE:BOOL=${THREADSAFE} \
-D SEACASIoss_ENABLE_THREADSAFE:BOOL=${THREADSAFE} \
\
${KOKKOS_SYMBOLS} \
${MPI_SYMBOLS} \
${DARWIN_OPT} \
${CEREAL_SYMBOLS} \
${FAODEL_SYMBOLS} \
\
-D MPI_BIN_DIR:PATH=${MPI_BIN} \
-D NetCDF_ROOT:PATH=${NETCDF_PATH} \
-D HDF5_ROOT:PATH=${HDF5_PATH} \
-D HDF5_NO_SYSTEM_PATHS=ON \
-D CGNS_ROOT:PATH=${CGNS_PATH} \
-D Matio_ROOT:PATH=${MATIO_PATH} \
-D PNetCDF_ROOT:PATH=${PNETCDF_PATH} \
-D PythonInterp_FIND_VERSION:STRING=${PYTHON_VER} \
\
$EXTRA_ARGS \
${ACCESS}

if [[ "$HAVE_FAODEL" == "YES" && "$HAVE_CEREAL" == "NO" ]] ; then
  echo "========================================================="
  echo "WARNING: Faodel is enabled, but Cereal is not."
  echo "WARNING: The Faodel components will fail to compile"
  echo "WARNING: unless Cereal is found implicitly."
  echo "========================================================="
fi

echo ""
echo "          OS: ${OS}"
echo "      ACCESS: ${ACCESS}"
echo " TPL_INSTALL: ${TPL_INSTALL}"
echo "INSTALL_PATH: ${INSTALL_PATH}"
echo "  "
echo "         CC: ${CC}"
echo "        CXX: ${CXX}"
echo "         FC: ${FC}"
echo "        MPI: ${MPI}"
echo "     SHARED: ${SHARED}"
echo " BUILD_TYPE: ${BUILD_TYPE}"
echo " THREADSAFE: ${THREADSAFE}"
echo " PYTHON_VER: ${PYTHON_VER}"
echo " "
echo "     NETCDF: ${HAVE_NETCDF}"
echo "      MATIO: ${HAVE_MATIO}"
echo "       CGNS: ${HAVE_CGNS}"
echo "     KOKKOS: ${HAVE_KOKKOS}"
echo "     ZOLTAN: ${ZOLTAN}"
echo "     ADIOS2: ${HAVE_ADIOS2}"
echo "     CEREAL: ${HAVE_CEREAL}"
echo "CEREAL_PATH: ${CEREAL_PATH}"
echo "     FAODEL: ${HAVE_FAODEL}"
echo "FAODEL_PATH: ${FAODEL_PATH}"
echo " Faodel_DIR: ${Faodel_DIR}"
echo "      GTEST: ${HAVE_GTEST}"
echo "    DOXYGEN: ${DOXYGEN}"
echo ""

if [ "${TRAVIS}" == "true" ]
then
  make -j2
  cd ${ACCESS}
fi
